package org.budo.gitlib.webhook;

import java.io.IOException;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.budo.support.mvcs.Mvcs;
import org.budo.support.servlet.util.ResponseUtil;
import org.budo.time.Time;

import lombok.extern.slf4j.Slf4j;

/**
 * @author limingwei
 */
@Slf4j
public class GitlibWebHookFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        log.info("#21 request=" + request);

        Map<String, String[]> requestHeaderMap = Mvcs.getRequestHeaderMap(request);
        log.info("#31 requestHeaderMap=" + requestHeaderMap);

        String requestBody = Mvcs.getRequestBody(request);
        log.info("#34 requestBody=" + requestBody);

        this.handlePushCommit();

        ResponseUtil.writeToResponse(response, Time.now().toString("yyyyMMdd.HHmmss.SSS"));
    }

    protected void handlePushCommit() {
        log.info("#43 handlePushCommit");
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        log.info("#21 init, this=" + this);
    }

    @Override
    public void destroy() {
        log.info("#29 destroy, this=" + this);
    }
}